export default {
  data() {
    return {
      pickerData: [
        []
      ],
      defaultIndex: [0],
      pickerShow: false,
      // 表示当前 picker 正在给那个 form 字段填充值
      pickerWithFormKey: "",
      // 表示当前正在操作那一个 form 
      pickerOperateForm: '',
      dictMapping: {}
    }
  },
  mounted() {
    this.initDict()
  },
  methods: {
    initDict() {
      let allCode = uni.getStorageSync("allCode");
      Object.assign(this.dictMapping, allCode);
    },
    /**
     * 字典值字段，表单字段 key， form 中 key 的 name
     */
    hadleShowPicker(dictName, formKey, formName = 'form') {
      this.pickerOperateForm = formName
      this.pickerWithFormKey = formKey;
      this.pickerData[0] = this.dictMapping[dictName] || [];
      let index = this.pickerData[0].findIndex(item => item.value == this[formName][formKey])
      if (~index) {
        this.defaultIndex = [index]
      } else {
        this.defaultIndex = [0]
      }
      this.pickerShow = true;
    },
    onPickerConfirm(data, formName = 'form') {
      let {
        value,
        text
      } = data.value[0];
      this[formName][this.pickerWithFormKey] = value;
      this.$set(this[formName], `${this.pickerWithFormKey}_text`, text);
      this.pickerShow = false;
    },
    pickerClose() {
      this.pickerShow = false;
    }
  }
}